package com.otod.pm.prj.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.web.BaseController;
import com.otod.pm.prj.service.TaskTpService;
import com.otod.pm.prj.vo.TaskTpVo;

/**
 * 任务模板管理
 * @author mamin
 */
@Controller
@RequestMapping("pm/taskTp")
public class TaskTpController extends BaseController {

	@Autowired
	private TaskTpService taskTpService;
	
	
	/**
	 * 新增任务
	 * @param taskTpVo
	 * @return
	 */
	@RequestMapping(value ="insert", method = RequestMethod.POST)
	@ResponseBody
	public TaskTpVo insert(TaskTpVo taskTpVo,HttpServletRequest request) {
		super.setInsertOperInfo(taskTpVo);
		return taskTpService.insert(taskTpVo);
	}

	/**
	 * 修改任务
	 * @param taskTpVo
	 * @return
	 */
	@RequestMapping(value ="update", method = RequestMethod.POST)
	@ResponseBody
	public TaskTpVo update(TaskTpVo vo,HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("uuid", vo.getUuid());
		map.put("taskName", vo.getTaskName());
		map.put("taskType", vo.getTaskType());
		super.setUpdateOperInfo(map);
		taskTpService.update(map);
		return vo;
	}
	
	/**
	 * 任务查询
	 * @param stepId
	 * @return
	 */
	@RequestMapping(value ="list", method = RequestMethod.POST)
	@ResponseBody
	public List<TaskTpVo> list(String stepTpId) {
		List<TaskTpVo> list = taskTpService.queryValidList(stepTpId);
		return list;
	}
	
	/**
	 * 任务删除
	 * @param uuid
	 * @return
	 */
	@RequestMapping(value ="delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(@RequestBody String uuid){
		taskTpService.deleteById(uuid);
		return JsonSuccess.factory("删除成功");
	}
	

	/**
	 * 主键查询
	 * @param id
	 * @throws AppException 
	 */
	@RequestMapping(value ="getById", method = RequestMethod.POST)
	@ResponseBody
	public TaskTpVo getById(@RequestBody String id) throws AppException{
		TaskTpVo vo = taskTpService.getById(id);
		if(vo == null){
			throw new AppException("任务不存在，可能已经被删除，请刷新！");
		}
		return vo;
	}
}
